<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Bon_de_commande extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('bon_de_commande_model');
        $this->load->model('detail_commande_model');
        $this->load->model('matiere_premiere_model');
        $this->load->model('client_model');
        $this->load->helper('date');
    }
    
    function index() {
        $SOCIETE_ID = $this->input->get('SOCIETE_ID');
        $bons_de_commandes = $this->bon_de_commande_model->getList($SOCIETE_ID, 0);
        $clients = $this->client_model->getList();
        
        $data = array(
            'bons_de_commandes' => $bons_de_commandes,
            'clients' => $clients,
            'SOCIETE_ID' => $SOCIETE_ID
        );
        
        $this->load->view('bon_de_commande/index', $data);
    }
    
    public function table($SOCIETE_ID, $COMMANDE_CLIENT) {
        $bons_de_commandes = $this->bon_de_commande_model->getList($SOCIETE_ID, $COMMANDE_CLIENT);
        return json_encode($bons_de_commandes);
    }
    
    /***************************************************************************
     * EDIT
     **************************************************************************/
    // Create
    public function create() {
        $SOCIETE_ID = $this->input->get('SOCIETE_ID');
        $COMMANDE_DATE = $this->input->get('COMMANDE_DATE');
        $COMMANDE_CLIENT = $this->input->get('COMMANDE_CLIENT');
        
        $data = array(
            'SOCIETE_ID' => $SOCIETE_ID,
            'COMMANDE_DATE' => $COMMANDE_DATE,
            'COMMANDE_CLIENT' => $COMMANDE_CLIENT
        );
        
        $this->load->view('bon_de_commande/edit', $data);
    }
    
    public function createDB() {
        $SOCIETE_ID = $this->input->get('SOCIETE_ID');
        $COMMANDE_DATE = $this->input->get('COMMANDE_DATE');
        $COMMANDE_CLIENT = $this->input->get('COMMANDE_CLIENT');
        $COMMANDE_DETAILS = $this->input->get('COMMANDE_DETAILS');
        
        $COMMANDE_ID = $this->bon_de_commande_model->create($SOCIETE_ID, $COMMANDE_DATE, $COMMANDE_CLIENT);
        
        foreach ($COMMANDE_DETAILS as $key => $COMMANDE_DETAIL) {
            $ARTICLE_ID = $COMMANDE_DETAIL['ARTICLE_ID'];
            $DETAIL_QUANTITE = $COMMANDE_DETAIL['DETAIL_QUANTITE'];
            $this->detail_commande_model->create($ARTICLE_ID, $COMMANDE_ID, $DETAIL_QUANTITE);
        }
        
        echo $this->table($SOCIETE_ID, $COMMANDE_CLIENT);
    }
    
    /***************************************************************************
     * PDF
     **************************************************************************/
    public function pdf(){
        $COMMANDE_ID = $this->input->get('COMMANDE_ID');
        $SOCIETE_ID = $this->input->get('SOCIETE_ID');
        
        $this->bon_de_commande_model->pdf($COMMANDE_ID, $SOCIETE_ID);
    }
}